# Add simple GUI for iOS security intrusion_Xamarin
The AppSealing library within your App is automatically activated when right after App has launched. If AppSealing library has detected any abnormal environment (such as jailbroken-device, executable has decrypted or debugger has attached) it will close the app after 20 seconds irrespectively of user action, so the app should notify the detection result to user and show some proper message box for user can recognize there's some invalid environment in his/her device.
# Show security alert UI in your app
Open the Xamarin project and select "ViewController.cs" file in the Project panel on the left. (Select the FirstViewController.cs file if it is a Tab or multiview based project)
Add the following line to the top of your ViewController.cs file.
using System.Runtime.InteropServices;
using System.Text;
Next, locate the ViewController class definition and insert the following code below it.
[System.Runtime.InteropServices.DllImport("__Internal")]
extern static public int Unity_IsAbnormalEnvironmentDetected();
[System.Runtime.InteropServices.DllImport("__Internal", CallingConvention = CallingConvention.Cdecl)]
extern static public int Unity_GetAppSealingDeviceID(StringBuilder deviceIDBuffer);
Your code should look like this:
Now go to bottom of the class definition and add following ViewDidAppear function code.
public override void ViewDidAppear (bool animated)
{
base.ViewDidAppear( animated );
StringBuilder appSealingDeviceID = new StringBuilder(64);
int result = 0;
result = Unity_GetAppSealingDeviceID(appSealingDeviceID);
if (result == 0)
{
string strAppSealingDeviceID = appSealingDeviceID.ToString();
System.Console.WriteLine("AppSealing DeviceID: " + strAppSealingDeviceID);
var okAlertController = UIAlertController.Create( "AppSealing DeviceID", strAppSealingDeviceID, UIAlertControllerStyle.Alert );
okAlertController.AddAction (UIAlertAction.Create ("OK", UIAlertActionStyle.Default, null));
//PresentViewController( okAlertController, true, null );
}
const int DETECTED_JAILBROKEN = 0x00000001;
const int DETECTED_DRM_DECRYPTED = 0x00000002;
const int DETECTED_DEBUG_ATTACHED = 0x00000004;
const int DETECTED_HASH_INFO_CORRUPTED = 0x00000008;
const int DETECTED_CODESIGN_CORRUPTED = 0x00000010;
const int DETECTED_HASH_MODIFIED = 0x00000020;
const int DETECTED_EXECUTABLE_CORRUPTED = 0x00000040;
const int DETECTED_CERTIFICATE_CHANGED = 0x00000080;
const int DETECTED_BLACKLIST_CORRUPTED = 0x00000100;
const int DETECTED_CHEAT_TOOL = 0x00000200;
try
{
result = Unity_IsAbnormalEnvironmentDetected();
if (result > 0)
{
// Show GUI
String msg = "Abnormal Environment Detected !!";
if ((result & DETECTED_JAILBROKEN) > 0)
msg += "\n - Jailbroken";
if ((result & DETECTED_DRM_DECRYPTED) > 0)
msg += "\n - Executable is not encrypted";
if ((result & DETECTED_DEBUG_ATTACHED) > 0)
msg += "\n - App is debugged";
if ((result & DETECTED_HASH_INFO_CORRUPTED) > 0 || (result & DETECTED_HASH_MODIFIED) > 0)
msg += "\n - App integrity has broken";
if ((result & DETECTED_CODESIGN_CORRUPTED) > 0 || (result & DETECTED_EXECUTABLE_CORRUPTED) > 0)
msg += "\n - App executable has corrupted";
if ((result & DETECTED_CERTIFICATE_CHANGED) > 0)
msg += "\n - App has re-signed";
if ((result & DETECTED_BLACKLIST_CORRUPTED) > 0)
msg += "\n - Blacklist corrupted or deleted";
if ((result & DETECTED_CHEAT_TOOL) > 0)
msg += "\n - Cheat tool detected";
Console.WriteLine("*** AppSealing Security Threat: " + msg);
//Create Alert
var okAlertController = UIAlertController.Create( "AppSealing Security Threat",
msg, UIAlertControllerStyle.Alert );
okAlertController.AddAction (UIAlertAction.Create ("OK", UIAlertActionStyle.Default,
(action) => System.Environment.Exit( 0 )));
PresentViewController( okAlertController, true, null );
}
}
catch (EntryPointNotFoundException e)
{
Console.WriteLine("### AppSealing Security Check : " + e.ToString());
}
}
If the 'ViewDidAppear' method is already included in the ViewController.cs file, Insert the code just below 'base.ViewDidAppear( animated );' line.
When your app is debugged or running the app on a abnormal device, the app displays a simple alert box like the one below. In this situation, regardless of user action, the app will automatically close after 20 seconds.
# Reminds about app build mode
- AppSealing work differently in debug mode and release mode.
If you build an app in Debug mode, AppSealing`s security features are disabled for convenient development :
Jailbreak detection
Anti-debugging
Not encrypted executable file detection
App-Integrity corruption detection
Re-signing detection
These features are enabled when you build app as Release mode.
You will build the app as Release mode when distributing to the App Store. If you test AppSealing with Release mode, your app should be distributed to App Store or 'TestFlight'. If not, the executable file will be detected as not encrypted, so the app will be closed.